<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible" width="65%">
    <!-- 列表 -->
    <ContentWrap>
      <el-table
        row-key="id"
        v-loading="loading"
        :data="list"
        :stripe="true"
        :show-overflow-tooltip="true"
        @selection-change="handleRowCheckboxChange"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column label="户号" align="center" prop="account" />
        <el-table-column label="户名" align="center" prop="name" />
      </el-table>
      <!-- 分页 -->
      <Pagination
        :total="total"
        v-model:page="queryParams.pageNo"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </ContentWrap>
    <template #footer>
      <el-button @click="submit" type="primary" :disabled="loading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>

<script setup lang="ts">
import { isEmpty } from '@/utils/is'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import {
  ExpandTargetPowerAccountApi,
  ExpandTargetPowerAccount
} from '@/api/biz/expand/targets/customer/poweraccount'
import ExpandTargetPowerAccountForm from './ExpandTargetPowerAccountForm.vue'
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'

/** 拓展目标_客户_用电户号 列表 */
defineOptions({ name: 'ExpandTargetPowerAccount' })

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题

/** 打开弹窗 */
const open = async (expandTargetCustomerId?: number) => {
  dialogVisible.value = true
  dialogTitle.value = '用电账户'
  queryParams.customerId = expandTargetCustomerId
  handleQuery()
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<ExpandTargetPowerAccount[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  account: undefined,
  name: undefined,
  powerType: undefined,
  voltageLevel: undefined,
  countMode: undefined,
  powerTimes: undefined,
  customerType: undefined,
  transformerAmount: undefined,
  transformerVolume: undefined,
  workingYears: undefined,
  maintenanceMode: undefined,
  isBreakdown: undefined,
  customerId: undefined,
  createTime: [],
  ownerUserId: undefined
})
const queryFormRef = ref() // 搜索的表单

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await ExpandTargetPowerAccountApi.getExpandTargetPowerAccountPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

const powerAccountData = ref([{}])
const handleRowCheckboxChange = (records: ExpandTargetPowerAccount[]) => {
  powerAccountData.value = records
}

const submit = () => {
  dialogVisible.value = false
  emit('success', powerAccountData.value)
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
